<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 该页面已完成
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input
          v-model="query.paramCode"
          clearable
          placeholder="编码"
          class="handle-input mr10"
        ></el-input>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button type="primary" @click="handleAdd">新增</el-button>
        <el-button type="danger" class="handle-del" @click="delAllSelection"
          >删除</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="paramCode"
          align="center"
          label="编码"
        ></el-table-column>
        <el-table-column
          prop="paramValue"
          label="值"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="remark"
          label="备注"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
      <el-form
        ref="editForm"
        :model="editForm"
        label-width="auto"
        :rules="rules"
      >
        <el-form-item label="编码" prop="paramCode">
          <el-input v-model="editForm.paramCode" clearable></el-input>
        </el-form-item>
        <el-form-item label="值" prop="paramValue">
          <el-input v-model="editForm.paramValue" clearable></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="editForm.remark" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 新增弹出框 -->
    <el-dialog title="新增" :visible.sync="addVisible" width="50%">
      <el-form ref="addForm" :model="addForm" label-width="auto" :rules="rules">
        <el-form-item label="编码" prop="paramCode">
          <el-input v-model="addForm.paramCode" clearable></el-input>
        </el-form-item>
        <el-form-item label="值" prop="paramValue">
          <el-input v-model="addForm.paramValue" clearable></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="addForm.remark" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { sysParamsMixin } from "common/js/systemSettings";
import util from "utils/util";
export default {
  name: "sysparams",
  mixins: [sysParamsMixin],
  created() {
    this.getSysParamsData({});
  },
  methods: {
    // 新增
    handleAdd() {
      this.addVisible = true;
    },
    saveAdd() {
      // 得到成功的提示
      this.$axios
        .post("/sys/params", {
          paramCode: this.addForm.paramCode,
          paramValue: this.addForm.paramValue,
          remark: this.addForm.remark,
          createDate: null,
          updateDate: null
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.addVisible = false;
          this.$message.success("操作成功");
          this.getSysParamsData({});
        });
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getSysParamsData({ paramCode: this.query.paramCode });
    },
    // 删除操作
    handleDelete(index, row) {
      this.ids[0] = row.id;
      util.delete.call(
        this,
        "/sys/params",
        index,
        this.ids,
        this.getSysParamsData
      );
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      var ids = this.multipleSelection.map(item => {
        return item.id;
      });
      util.delete.call(this, "/sys/params", "", ids, this.getSysParamsData);
    },
    // 编辑操作
    handleEdit(index, row) {
      this.idx = index;
      this.editForm = row;
      this.editVisible = true;

      this.$axios
        .get("/sys/params/" + row.id, {
          _t: Date.now()
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.addForm = res.data;
        })
        .catch(() => {});
    },
    // 保存编辑
    saveEdit() {
      // 得到成功提示
      this.$axios
        .put("/sys/params", {
          id: this.editForm.id,
          paramCode: this.editForm.paramCode,
          paramValue: this.editForm.paramValue,
          remark: this.editForm.remark,
          createDate: this.editForm.createDate,
          updateDate: this.editForm.updateDate
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.editVisible = false;
          this.$message.success("操作成功");
          this.getSysParamsData({});
        })
        .catch(() => {});
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getSysParamsData({});
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getSysParamsData();
    }
  }
};
</script>

<style scoped>
@import "../../common/css/index.css";
.mr10 {
  width: 195px;
}
</style>
